package com.example.demo.filter;



import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "ResponseFilter", urlPatterns = "/*")
public class ResponseFilter implements Filter {

/*    @Autowired
    private ISysAllconfigService iSysAllconfigService;
    @Autowired
    private Redis2Service redis2Service;*/


    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        ResponseWrapper wrapper = new ResponseWrapper((HttpServletResponse) response);
        HttpServletRequest req = (HttpServletRequest) request;

        chain.doFilter(request, wrapper);


        // 获取response返回的内容并重新写入response

        String uri = req.getRequestURI();

        String result = wrapper.getResponseData(response.getCharacterEncoding());

       // System.out.println("返回信息:"+result);
        /*if(
                        uri.equals("/web_company/list") ||
                        uri.equals("/web_company/getById") ||
                        uri.equals("/web_company/getById1") ||
                        uri.equals("/web_address/getByCompanyId") ||
                        uri.equals("/web_address/getById") ||
                        uri.equals("/web_onlinestudy_type/getListByOnlineId") ||
                        uri.equals("/web_course/getAddress") ||
                        uri.equals("/web_course/getById") ||
                         uri.equals("/web_course/getLowPrice") ||
                         uri.equals("/web_course/list")

        ){
            Object adminTel1 = redis2Service.get("adminTel");
            String tel="";
            if(adminTel1!=null){
                tel = (String)adminTel1;
            }else{
                SysAllconfig adminTel = iSysAllconfigService.getByCode("0", "adminTel");
                tel = adminTel.getVal();
                redis2Service.set("adminTel",tel);
            }
            result = result.replaceAll("(?<=\"tel\":\")[^\",]*", tel);
        }*/


        response.getOutputStream().write(result.getBytes());


    }

    @Override
    public void init(FilterConfig fConfig) throws ServletException {

    }

}